import axios from 'axios'
import { getToken } from '@/utils/auth'

// 创建axios实例
const service = axios.create({
  baseURL: 'https://recruit.aceo.me/api',
  timeout: 5000
})

// 登录方法
export function login(username, password) {
  const data = new FormData()
  data.append('mobile', username)
  data.append('password', password)

  return service({
    method: 'post',
    url: '/boss/login',
    data: data
  })
}

// 请求拦截器
service.interceptors.request.use(
  config => {
    if (getToken()) {
      config.headers['Authorization'] = 'Bearer ' + getToken()
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    // 这里可以处理响应数据
    return res
  },
  error => {
    // 这里可以处理错误响应
    return Promise.reject(error)
  }
)

export default service
